package com.itheima.controller;


import com.itheima.pojo.Clazz;
import com.itheima.pojo.ClazzQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/clazzs")
public class ClazzController {

    @Autowired
    private ClazzService clazzService;

    /**
     * 添加班级
     * @param clazz
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Clazz clazz) {
        log.info("添加班级 = " + clazz);
        clazzService.add(clazz);
        return Result.success();
    }

    /**
     * 查询所有班级
     * @return
     */
    @GetMapping("/list")
    public Result list(){
        log.info("查询所有班级");
        List<Clazz> clazzList = clazzService.list();
        return Result.success(clazzList);
    }

    /**
     * 班级分页查询
     * 班级列表查询
     * @param clazzQueryParam
     * @return
     */
    @GetMapping
    public Result page(ClazzQueryParam clazzQueryParam) {
        log.info("分页查询，参数：" + clazzQueryParam);
        PageResult<Clazz> pageResult = clazzService.page(clazzQueryParam);
        return Result.success(pageResult);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("删除班级,班级ID:" + id);
        clazzService.deleteById(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result get(@PathVariable Integer id){
        log.info("查询班级,班级ID:" + id);
        Clazz clazz = clazzService.getById(id);
        return Result.success(clazz);
    }

    @Transactional
    @PutMapping
    public Result update(@RequestBody Clazz clazz){
        log.info("更新班级信息：{}", clazz);
        clazzService.update(clazz);
        return Result.success();
    }
    @RequestMapping
    public Result findAll() {
        List<Clazz> list = clazzService.findAll();
        return Result.success(list);
    }
}

